<template>
  <div class="GoodsAdd">
    <el-steps :active="active" finish-status="success">
      <el-step title="步骤 1" description="基本信息"></el-step>
      <el-step title="步骤 2" description="商品图片"></el-step>
      <el-step title="步骤 3" description="商品内容"></el-step>
    </el-steps>

    <!-- tab栏 -->
    <el-tabs v-model="activeName" tab-position="left" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="0">
        <el-form :model="form" label-width="80px">
          <el-form-item label="商品名称">
            <el-input placeholder="请输入商品名称" v-model="form.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input placeholder="请输入商品价格" v-model="form.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量">
            <el-input placeholder="请输入商品重量" v-model="form.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量">
            <el-input placeholder="请输入商品数量" v-model="form.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader v-model="form.goods_cat" :options="options" :props="props"></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="next">下一步</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="商品图片" name="1">
          <!--
          action: 上传图片的地址.防止以后再更改。写成动态的
          name: 图片上传后台需要的name,需要和后台一样。
          headers: 请求头
          multiple :多选
         -->
        <el-upload
          name="file"
          :action="$axios.defaults.baseURL + 'upload'"
          :headers="headers"
          multiple
          list-type="picture-card"
          :on-success="handleSucces"
          :before-upload="beforeAvatarUpload"
          :on-remove="handleRemove"
           :on-preview="handlePictureCardPreview"

        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <el-button type="primary" @click="next">下一步</el-button>
      </el-tab-pane>

      <el-tab-pane label="商品内容" name="2">
         <quill-editor v-model="form.goods_introduce"></quill-editor>
          <el-button @click="addGoods" type="primary">添加</el-button>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  data () {
    return {
      active: 0,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        goods_name: '',
        goods_cat: [],
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: '',
        pics: []

      },
      options: [],
      props: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      activeName: '0',
      headers: {
        Authorization: localStorage.getItem('token')
      }
    }
  },
  methods: {
    handleClick (tab) {
      console.log(tab)

      this.active = +tab.index
    },
    next () {
      this.active++
      this.activeName = this.active + ''
    },
    // 保存图片信息， 添加到PICS空数组里面
    handleSucces (response, file, fileList) {
      const { meta, data } = response
      if (meta.status === 200) {
        this.form.pics.push({
          pic: data.tmp_path
        })
      }
    },
    // 设置图片上传格式
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 png 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 上传图片删除时的钩子函数
    handleRemove (file, fileList) {
    //   console.log(file)
    //   console.log(fileList)
      const path = file.response.data.tmp_path
      console.log(path)
      this.form.pics = this.form.pics.filter(item => item.pic !== path)
    },
    // 图片预览功能
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 提交数据
    async addGoods () {
      const { meta } = await this.$axios.post('goods', { ...this.form,
        goods_cat: this.form.goods_cat.join(',') })
      // console.log(res)
      if (meta.status === 201) {
        this.$message.success('添加成功')
        this.$router.push('/goods')
      } else {
        this.$message.error(meta.msg)
      }
    }
  },

  /* ------------------------------------------------------------------- */
  async created () {
    const { meta, data } = await this.$axios.get('categories?type=3')
    if (meta.status === 200) {
      this.options = data
    }
  }

}
</script>

<style lang='less' scoped>
.quill-editor {
  background-color: #fff;
  /deep/ .ql-editor {
    height: 300px;
  }
}
</style>
